
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>winsound --- Sound-playing interface for Windows &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="Unix 专有服务" href="unix.html" />
    <link rel="prev" title="winreg --- Windows 注册表访问" href="winreg.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/winsound.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="unix.html" title="Unix 专有服务"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="winreg.html" title="winreg --- Windows 注册表访问"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="windows.html" accesskey="U">Windows系统相关模块</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-winsound">
<span id="winsound-sound-playing-interface-for-windows"></span><h1><a class="reference internal" href="#module-winsound" title="winsound: Access to the sound-playing machinery for Windows. (Windows)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">winsound</span></code></a> --- Sound-playing interface for Windows<a class="headerlink" href="#module-winsound" title="永久链接至标题">¶</a></h1>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-winsound" title="winsound: Access to the sound-playing machinery for Windows. (Windows)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">winsound</span></code></a> module provides access to the basic sound-playing machinery
provided by Windows platforms.  It includes functions and several constants.</p>
<dl class="function">
<dt id="winsound.Beep">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">Beep</code><span class="sig-paren">(</span><em class="sig-param">frequency</em>, <em class="sig-param">duration</em><span class="sig-paren">)</span><a class="headerlink" href="#winsound.Beep" title="永久链接至目标">¶</a></dt>
<dd><p>Beep the PC's speaker. The <em>frequency</em> parameter specifies frequency, in hertz,
of the sound, and must be in the range 37 through 32,767. The <em>duration</em>
parameter specifies the number of milliseconds the sound should last.  If the
system is not able to beep the speaker, <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.</p>
</dd></dl>

<dl class="function">
<dt id="winsound.PlaySound">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">PlaySound</code><span class="sig-paren">(</span><em class="sig-param">sound</em>, <em class="sig-param">flags</em><span class="sig-paren">)</span><a class="headerlink" href="#winsound.PlaySound" title="永久链接至目标">¶</a></dt>
<dd><p>Call the underlying <code class="xref c c-func docutils literal notranslate"><span class="pre">PlaySound()</span></code> function from the Platform API.  The
<em>sound</em> parameter may be a filename, a system sound alias, audio data as a
<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>, or <code class="docutils literal notranslate"><span class="pre">None</span></code>.  Its
interpretation depends on the value of <em>flags</em>, which can be a bitwise ORed
combination of the constants described below. If the <em>sound</em> parameter is
<code class="docutils literal notranslate"><span class="pre">None</span></code>, any currently playing waveform sound is stopped. If the system
indicates an error, <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.</p>
</dd></dl>

<dl class="function">
<dt id="winsound.MessageBeep">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">MessageBeep</code><span class="sig-paren">(</span><em class="sig-param">type=MB_OK</em><span class="sig-paren">)</span><a class="headerlink" href="#winsound.MessageBeep" title="永久链接至目标">¶</a></dt>
<dd><p>Call the underlying <code class="xref c c-func docutils literal notranslate"><span class="pre">MessageBeep()</span></code> function from the Platform API.  This
plays a sound as specified in the registry.  The <em>type</em> argument specifies which
sound to play; possible values are <code class="docutils literal notranslate"><span class="pre">-1</span></code>, <code class="docutils literal notranslate"><span class="pre">MB_ICONASTERISK</span></code>,
<code class="docutils literal notranslate"><span class="pre">MB_ICONEXCLAMATION</span></code>, <code class="docutils literal notranslate"><span class="pre">MB_ICONHAND</span></code>, <code class="docutils literal notranslate"><span class="pre">MB_ICONQUESTION</span></code>, and <code class="docutils literal notranslate"><span class="pre">MB_OK</span></code>, all
described below.  The value <code class="docutils literal notranslate"><span class="pre">-1</span></code> produces a &quot;simple beep&quot;; this is the final
fallback if a sound cannot be played otherwise.  If the system indicates an
error, <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a> is raised.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.SND_FILENAME">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">SND_FILENAME</code><a class="headerlink" href="#winsound.SND_FILENAME" title="永久链接至目标">¶</a></dt>
<dd><p>The <em>sound</em> parameter is the name of a WAV file. Do not use with
<a class="reference internal" href="#winsound.SND_ALIAS" title="winsound.SND_ALIAS"><code class="xref py py-const docutils literal notranslate"><span class="pre">SND_ALIAS</span></code></a>.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.SND_ALIAS">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">SND_ALIAS</code><a class="headerlink" href="#winsound.SND_ALIAS" title="永久链接至目标">¶</a></dt>
<dd><p>The <em>sound</em> parameter is a sound association name from the registry.  If the
registry contains no such name, play the system default sound unless
<a class="reference internal" href="#winsound.SND_NODEFAULT" title="winsound.SND_NODEFAULT"><code class="xref py py-const docutils literal notranslate"><span class="pre">SND_NODEFAULT</span></code></a> is also specified. If no default sound is registered,
raise <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>. Do not use with <a class="reference internal" href="#winsound.SND_FILENAME" title="winsound.SND_FILENAME"><code class="xref py py-const docutils literal notranslate"><span class="pre">SND_FILENAME</span></code></a>.</p>
<p>All Win32 systems support at least the following; most systems support many
more:</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 39%" />
<col style="width: 61%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p><a class="reference internal" href="#winsound.PlaySound" title="winsound.PlaySound"><code class="xref py py-func docutils literal notranslate"><span class="pre">PlaySound()</span></code></a> <em>name</em></p></th>
<th class="head"><p>Corresponding Control Panel Sound name</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'SystemAsterisk'</span></code></p></td>
<td><p>Asterisk</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'SystemExclamation'</span></code></p></td>
<td><p>Exclamation</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'SystemExit'</span></code></p></td>
<td><p>Exit Windows</p></td>
</tr>
<tr class="row-odd"><td><p><code class="docutils literal notranslate"><span class="pre">'SystemHand'</span></code></p></td>
<td><p>Critical Stop</p></td>
</tr>
<tr class="row-even"><td><p><code class="docutils literal notranslate"><span class="pre">'SystemQuestion'</span></code></p></td>
<td><p>Question</p></td>
</tr>
</tbody>
</table>
<p>例如</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">winsound</span>
<span class="c1"># Play Windows exit sound.</span>
<span class="n">winsound</span><span class="o">.</span><span class="n">PlaySound</span><span class="p">(</span><span class="s2">&quot;SystemExit&quot;</span><span class="p">,</span> <span class="n">winsound</span><span class="o">.</span><span class="n">SND_ALIAS</span><span class="p">)</span>

<span class="c1"># Probably play Windows default sound, if any is registered (because</span>
<span class="c1"># &quot;*&quot; probably isn&#39;t the registered name of any sound).</span>
<span class="n">winsound</span><span class="o">.</span><span class="n">PlaySound</span><span class="p">(</span><span class="s2">&quot;*&quot;</span><span class="p">,</span> <span class="n">winsound</span><span class="o">.</span><span class="n">SND_ALIAS</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="data">
<dt id="winsound.SND_LOOP">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">SND_LOOP</code><a class="headerlink" href="#winsound.SND_LOOP" title="永久链接至目标">¶</a></dt>
<dd><p>Play the sound repeatedly.  The <a class="reference internal" href="#winsound.SND_ASYNC" title="winsound.SND_ASYNC"><code class="xref py py-const docutils literal notranslate"><span class="pre">SND_ASYNC</span></code></a> flag must also be used to
avoid blocking.  Cannot be used with <a class="reference internal" href="#winsound.SND_MEMORY" title="winsound.SND_MEMORY"><code class="xref py py-const docutils literal notranslate"><span class="pre">SND_MEMORY</span></code></a>.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.SND_MEMORY">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">SND_MEMORY</code><a class="headerlink" href="#winsound.SND_MEMORY" title="永久链接至目标">¶</a></dt>
<dd><p>The <em>sound</em> parameter to <a class="reference internal" href="#winsound.PlaySound" title="winsound.PlaySound"><code class="xref py py-func docutils literal notranslate"><span class="pre">PlaySound()</span></code></a> is a memory image of a WAV file, as a
<a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>.</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>This module does not support playing from a memory image asynchronously, so a
combination of this flag and <a class="reference internal" href="#winsound.SND_ASYNC" title="winsound.SND_ASYNC"><code class="xref py py-const docutils literal notranslate"><span class="pre">SND_ASYNC</span></code></a> will raise <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.</p>
</div>
</dd></dl>

<dl class="data">
<dt id="winsound.SND_PURGE">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">SND_PURGE</code><a class="headerlink" href="#winsound.SND_PURGE" title="永久链接至目标">¶</a></dt>
<dd><p>Stop playing all instances of the specified sound.</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>This flag is not supported on modern Windows platforms.</p>
</div>
</dd></dl>

<dl class="data">
<dt id="winsound.SND_ASYNC">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">SND_ASYNC</code><a class="headerlink" href="#winsound.SND_ASYNC" title="永久链接至目标">¶</a></dt>
<dd><p>Return immediately, allowing sounds to play asynchronously.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.SND_NODEFAULT">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">SND_NODEFAULT</code><a class="headerlink" href="#winsound.SND_NODEFAULT" title="永久链接至目标">¶</a></dt>
<dd><p>If the specified sound cannot be found, do not play the system default sound.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.SND_NOSTOP">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">SND_NOSTOP</code><a class="headerlink" href="#winsound.SND_NOSTOP" title="永久链接至目标">¶</a></dt>
<dd><p>Do not interrupt sounds currently playing.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.SND_NOWAIT">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">SND_NOWAIT</code><a class="headerlink" href="#winsound.SND_NOWAIT" title="永久链接至目标">¶</a></dt>
<dd><p>Return immediately if the sound driver is busy.</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>This flag is not supported on modern Windows platforms.</p>
</div>
</dd></dl>

<dl class="data">
<dt id="winsound.MB_ICONASTERISK">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">MB_ICONASTERISK</code><a class="headerlink" href="#winsound.MB_ICONASTERISK" title="永久链接至目标">¶</a></dt>
<dd><p>Play the <code class="docutils literal notranslate"><span class="pre">SystemDefault</span></code> sound.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.MB_ICONEXCLAMATION">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">MB_ICONEXCLAMATION</code><a class="headerlink" href="#winsound.MB_ICONEXCLAMATION" title="永久链接至目标">¶</a></dt>
<dd><p>Play the <code class="docutils literal notranslate"><span class="pre">SystemExclamation</span></code> sound.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.MB_ICONHAND">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">MB_ICONHAND</code><a class="headerlink" href="#winsound.MB_ICONHAND" title="永久链接至目标">¶</a></dt>
<dd><p>Play the <code class="docutils literal notranslate"><span class="pre">SystemHand</span></code> sound.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.MB_ICONQUESTION">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">MB_ICONQUESTION</code><a class="headerlink" href="#winsound.MB_ICONQUESTION" title="永久链接至目标">¶</a></dt>
<dd><p>Play the <code class="docutils literal notranslate"><span class="pre">SystemQuestion</span></code> sound.</p>
</dd></dl>

<dl class="data">
<dt id="winsound.MB_OK">
<code class="sig-prename descclassname">winsound.</code><code class="sig-name descname">MB_OK</code><a class="headerlink" href="#winsound.MB_OK" title="永久链接至目标">¶</a></dt>
<dd><p>Play the <code class="docutils literal notranslate"><span class="pre">SystemDefault</span></code> sound.</p>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>上一个主题</h4>
  <p class="topless"><a href="winreg.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">winreg</span></code> --- Windows 注册表访问</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="unix.html"
                        title="下一章">Unix 专有服务</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/winsound.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="unix.html" title="Unix 专有服务"
             >下一页</a> |</li>
        <li class="right" >
          <a href="winreg.html" title="winreg --- Windows 注册表访问"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="windows.html" >Windows系统相关模块</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>